/*
 * Project: Code
 * File Created at 2024/1/13
 @Type Assign_cookies.java
 @author Goodwin
 * Website : https://leetcode.cn/problems/assign-cookies/description/
*/

package Greedy_Algo;

import java.util.Arrays;

public class Assign_cookies {
    public int solution(int[] g, int[] s){
//        简单的贪心算法，只要不断的将饼干分给刚好满足这个孩子中最大的那个就好
//        双指针 遍历双数组
        Arrays.sort(g); // 从小到大
        Arrays.sort(s);

        int m = g.length, n = s.length;
        int count = 0;

        for (int i = 0, j = 0; i < m && j < n; i++, j++) {
            while (j < n && g[i] > s[j]) {
                j++;
            }
            if (j < n) {
                count++;
            }
        }
        return count;
    }
}
